<?php

namespace app\admin\controller;

use think\Controller;
use OSS\OssClient;
use think\Db;
use think\Request;

class Index extends Basecontroller
{


    public function index()
    {

        $sql = "select count(id) as count from patient where to_days(create_time) = to_days(now())  UNION  ALL 
                select count(id)  as count from patient where TO_DAYS(NOW()) - TO_DAYS(create_time) = 1 UNION  ALL 
                select count(id)  as count from patient where DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
              ";
        $patient_count = Db::query($sql);

        $patient_case_sql = "select count(id) as count from patient_case where to_days(create_time) = to_days(now())  UNION  ALL 
                 select count(id)  as count from patient_case where TO_DAYS(NOW()) - TO_DAYS(create_time) = 1  UNION  ALL 
                 select count(id)  as count from patient_case where DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
                ";
        $patient_case_count = Db::query($patient_case_sql);

        $user_sql = "select count(id) as count from user where to_days(create_time) = to_days(now())  UNION  ALL 
                 select count(id)  as count from user where TO_DAYS(NOW()) - TO_DAYS(create_time)  =  1  UNION  ALL 
                 select count(id)  as count from user where DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
 ";
        $user_count = Db::query($user_sql);

        $feedback_sql= "select count(*) as count from feedback where is_read=0";
        $feedback_count = Db::query($feedback_sql);
       //患者统计
        $this->assign('patient_count', $patient_count);
        //患者记录
        $this->assign('patient_case_count', $patient_case_count);
        //用户记录
        $this->assign('user_count', $user_count);
        //未读消息
        $this->assign('feedback_count', $feedback_count);

        return $this->fetch();
    }





    /**
     * 设置用户
    **/
    public function set_user(){

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $old_password = md5($data['old_password']);
            $new_password = $data['new_password'];
            $confirm_password = $data['confirm_password'];
            $userId = $data['userId'];

            if($data['old_password'] && $new_password){
                $userInfo = DB::name('admin')->where("admin_id={$userId} and admin_password='{$old_password}'")->find();
                // print_r(DB::name('admin')->getLastSql());
                if(!$userInfo){
                    return $this->error("旧密码错误");
                }


                if($new_password != $confirm_password){
                    return $this->error("两次密码不一致");
                }

                $save_data['admin_password'] =  MD5($confirm_password);
            }

            if(isset($data['avatar'])){
                $save_data['avatar'] = $data['avatar'];
            }

            $userInfo = DB::name('admin')->where("admin_id={$userId}")->update($save_data);
            if($userInfo){
                return $this->success("操作成功",url('index/index'));
            }else{
                return $this->error("两次密码不一致");
            }

        }




        //根据当前的登录者,获取其相关信息
        $userId = session("adminuid");
        $userInfo = DB::name('admin')
            ->join('doctor_department','doctor_department.id=admin.depart_id')
                ->where("admin_id={$userId}")->find();

        $this->assign('userInfo', $userInfo);
        $this->assign('breadcrumb', '账户设置');
       return  $this->fetch();
    }


}
